package com.xdtech.base.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.base.model.Province;
import com.xdtech.base.service.ProvinceService;
import com.xdtech.base.vo.ProvinceItem;
import com.xdtech.common.fomat.TreeBuilder;
import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;
import com.xdtech.web.model.TreeItem;

/**
 * 
 * @author max.zheng
 * @create 2015-06-02 14:00:50
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class ProvinceController {
	private Log log = LogFactory.getLog(ProvinceController.class);
	@Autowired
	private ProvinceService provinceService;
	@RequestMapping(value="/province.do",params = "province")
	public ModelAndView skipProvince() {
		return new ModelAndView("base/province/province_ftl");
	}
	
	@RequestMapping(value="/province.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(ProvinceItem item,Pagination pg) {
		Map<String, Object> results =  provinceService.loadPageDataByConditions(pg,item,"findProvinceByCondition");
		return results;
	}
	
	@RequestMapping(value="/province.do",params = "editProvince")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editProvince(HttpServletRequest request,Long provinceId) {
		if (provinceId!=null) {
			request.setAttribute("provinceItem", provinceService.loadProvinceItem(provinceId));
		}
		return new ModelAndView("base/province/editProvince_ftl");
	}
	
	@RequestMapping(value="/province.do",params = "saveProvince")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveProvince(ProvinceItem item) {
		ResultMessage r = new ResultMessage();
		if (provinceService.saveOrUpdateProvince(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/province.do",params = "deleteProvinceItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteProvinceItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> provinceIds = new ArrayList<Long>();
			for (String id : tempIds) {
				provinceIds.add(Long.valueOf(id));
			}
			provinceService.deleteProvinceInfo(provinceIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

	@RequestMapping(value="/province.do",params = "provinceTree")
	@ResponseBody
	public List<TreeItem> provinceTree(HttpServletRequest request) {
		List<TreeItem> items = new ArrayList<TreeItem>();
		List<Province> provinces = provinceService.getAll();
		TreeItem item = null;
		TreeItem pItem = null;
		for (Province province : provinces)
		{
			item = new TreeItem();
			item.setId(province.getId());
			item.setText(province.getName());
			item.setContent(province.getCode());
			item.setParent(pItem);
			items.add(item);
		}
		return TreeBuilder.newTreeBuilder(TreeItem.class, Long.class).buildToTreeList(items);
	}
	
	@RequestMapping(params = "loadComboBox")
	@ResponseBody
	public Object loadComboBox(String value) {
		List<ComboBox> comboBoxsTemp = new ArrayList<ComboBox>();
		List<Province> provinces = provinceService.getAll();
		for (Province province : provinces) {
			comboBoxsTemp.add(new ComboBox(province.getId()+"", province.getName(), province.getId().toString().equals(value)));
		}
		return comboBoxsTemp;
	}
}
